﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace myschoolDal
{
    /// <summary>
    /// 提供管理员所需的界面与数据库交互
    /// </summary>
    public class manserver
    {
        /*
         * 数据库连接字符串
         * 使用前在这里连接好自己的数据库
         */
         public static string strcon = "Data source = .; Initial Catalog = myschool;Integrated Security = true";
        
        //private static string sqlmaxnum = "select max(stunum) from student";
        //private static string sqlidpasd = "SELECT manid, manpasswd FROM manager WHERE manid = '" + Id + "' AND manpasswd = '" + Psd + "'";

        #region 检查管理员登录信息
        /// <summary>
        /// 检查管理员登录信息是否存在，密码是否正确
        /// </summary>
        /// <param name="manLoginId"></param>
        /// <param name="manLoginPassword"></param>
        /// <returns> true 账号存在，密码正确； false 账号不存在或者密码不正确</returns>
        public static bool ManLoginCheck(long Id, string Psd)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //设置数据库语句
                    string sqlcheckidpassword = "SELECT manid,manpasswd FROM manager WHERE manid = '" + Id + "' AND manpasswd = '" + Psd + "'";
                    //在SQL数据库查询相关信息
                    SqlDataAdapter adp = new SqlDataAdapter(sqlcheckidpassword, conn);
                    //建立DataSet类ds，用于储存信息
                    DataSet ds = new DataSet();
                    ds.Clear();
                    //填充相关信息
                    adp.Fill(ds);

                    //如果查询到相关信息，即ds.Tables[0].Rows.Count不为0 返回TRUE，反之返回FALSE
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        //管理员信息存在，返回允许登录
                        return true;
                    }
                    else
                    {
                        //管理员信息不存在，不允许登录
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return false;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 添加学生信息
        /// <summary>
        /// 用于添加学生信息
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="name"></param>
        /// <param name="major"></param>
        /// <param name="grade"></param>
        /// <param name="sex"></param>
        /// <param name="passwd"></param>
        public static void AddStuInformation(long ID, string name, string major, string grade, string sex, string passwd)
        {
            //新建连接数据库
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };

                try
                {
                    int num;
                    //打开数据库

                    //设置数据库语句
                    string sql1 = "select max(stunum) from student";
                    //在SQL数据库查询相关信息
                    //SqlDataAdapter adp = new SqlDataAdapter(sql1, conn);
                    //DataSet ds = new DataSet();
                    //ds.Clear();
                    //填充相关信息
                    //adp.Fill(ds);
                    //关闭数据库
                    SqlCommand cmd1 = new SqlCommand(sql1, conn);
                    //SqlDataReader read = cmd1.ExecuteReader();

                    //获取当前最新的编号
                    num = Convert.ToInt32(cmd1.ExecuteScalar());
                    //编号自增，用于建立新编号
                    num++;
                    //SQL语句 插入学生信息
                    string sql = "insert into student(stunum, stuname, stuid, stupassword, stugrade, stumajor, stusex) values(" + num + ", '" + name + "', " + ID + ", '" + passwd + "', '" + grade + "', '" + major + "', '" + sex + "');";
                    //连接数据库
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    //改变数据库的连接信息
                    cmd.CommandText = sql;
                    //执行
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 检查ID       
        /// <summary>
        /// 检查管理员id
        /// </summary>
        /// <param name="id"></param>
        /// <returns>true 账号存在； false 账号不存在</returns>
        public static bool CheckId(long id)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //SqlCommand cmd = new SqlCommand("SELECT stuid,stupassword FROM stu WHERE stuid = '" + stuLoginId + "' AND stupassword = " + stuLoginPassword, conn);
                    string sqlcheckid = "SELECT manid FROM manager WHERE manid = " + id + ";";
                    SqlDataAdapter adp = new SqlDataAdapter(sqlcheckid, conn);
                    //获取数据
                    DataSet ds = new DataSet();
                    ds.Clear();
                    adp.Fill(ds);
                    //conn.Close();
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return false;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 添加管理员生信息
        /// <summary>
        /// 添加管理员信息， 包括ID，name， password
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="name"></param>
        /// <param name="passwd"></param>
        public static void AddManInformation(long ID, string name, string passwd)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    int num;
                    //打开数据库
                    string sql1 = "select max(stunum) from student";

                    SqlCommand cmd1 = new SqlCommand(sql1, conn);

                    num = Convert.ToInt32(cmd1.ExecuteScalar());
                    num++;

                    //数据库查询语句
                    string sql = "insert into manager(manid, manname, manpasswd) values(" + ID + ", '" + name + "','" + passwd + "');";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    //更新数据库语句
                    cmd.CommandText = sql;
                    //更新数据库
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);

                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 修改管理员账号和密码
        /// <summary>
        /// 根据管理员ID修改管理员name， passwd
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="name"></param>
        /// <param name="passwd"></param>
        public static void ChangeManInformation(long ID, string name, string passwd)
        {

            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //数据库查询语句
                    string sql = "update manager SET manname = '" + name + "' where manid = " + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 删除学生信息
        /// <summary>
        /// 根据学生学号，删除某个学生在数据库中的信息
        /// </summary>
        /// <param name="ID"></param>
        public static void DeleteStuInformation(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //数据库查询语句
                    string sql = "delete from student where stuid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 删除管理员信息
        /// <summary>
        /// 用于删除管理员信息
        /// </summary>
        /// <param name="ID"></param>
        public static void DeleteManInformation(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //数据库查询语句
                    string sql = "delete from manager where manid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取学生name
        /// <summary>
        /// 在有学生学号的条件下，获取学生姓名
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static string GetStuInformationName(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    string name;
                    //数据库查询语句
                    string sql = "select stuname from student where stuid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    //SqlDataReader read = cmd.ExecuteReader();
                    //cmd.CommandText = sql;
                    //cmd.ExecuteNonQuery();
                    //name = read["stuname"].ToString();
                    name = cmd.ExecuteScalar().ToString();
                    //MessageBox.Show(name);                  

                    return name;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取学生sex
        /// <summary>
        /// 在有学生学号的条件下，获取学生性别
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static string GetStuInformationSex(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    string sex;
                    //int num;
                    //
                    //数据库查询语句
                    string sql = "select stusex from student where stuid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    sex = cmd.ExecuteScalar().ToString();

                    return sex;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取学生major
        /// <summary>
        /// 在有学生学号的条件下，获取学生专业
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static string GetStuInformationMajor(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    string major;
                    //int num;                

                    //数据库查询语句
                    string sql = "select stumajor from student where stuid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    //cmd.ExecuteNonQuery();
                    major = cmd.ExecuteScalar().ToString();

                    return major;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取学生grade
        /// <summary>
        /// 在有学生学号的条件下，获取学生年级
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static string GetStuInformationGrade(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    string grade;
                    //int num;

                    //数据库查询语句
                    string sql = "select stugrade from student where stuid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    grade = cmd.ExecuteScalar().ToString();

                    return grade;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取管理员name
        /// <summary>
        /// 在有管理员账号的条件下，获取管理员姓名
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static string GetManInformationName(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    string name;
                    //数据库查询语句
                    string sql = "select manname from manager where manid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    //SqlDataReader read = cmd.ExecuteReader();
                    //cmd.CommandText = sql;
                    //cmd.ExecuteNonQuery();
                    //name = read["stuname"].ToString();
                    name = cmd.ExecuteScalar().ToString();
                    return name;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取管理员psd
        /// <summary>
        /// 在有管理员账号的条件下，获取管理员登录密码
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static string GetManInformationPsd(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    string psd;

                    //数据库查询语句
                    string sql = "select manpasswd from manager where manid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    psd = cmd.ExecuteScalar().ToString();

                    return psd;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 管理员修改密码
        /// <summary>
        /// 修改管理员密码
        /// </summary>
        /// <param name="newmanPassword_1"></param>
        /// <param name="newmanPassword_2"></param>
        public static void ManChangePassword(string newManPassword, long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    string sqlchagestupassword = "update manager set manpasswd ='" + newManPassword + "' where manid = '" + ID + "'";
                    SqlDataAdapter adp = new SqlDataAdapter(sqlchagestupassword, conn);
                    cmd.CommandText = sqlchagestupassword;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取学生num
        /// <summary>
        /// 在有学生学号的情况下，获取学生的编号
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int GetStuInformationNum(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    int num;

                    //数据库查询语句
                    string sql = "select stusex from student where stuid =" + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    num = Convert.ToInt32(cmd.ExecuteScalar());

                    return num;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return -1;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取学生id
        /// <summary>
        /// 在同时有学生姓名，专业的条件下，获取学生的学号
        /// </summary>
        /// <param name="name"></param>
        /// <param name="major"></param>
        /// <returns>long</returns>
        public static long GetStuInformationID(string name, string major)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    long ID;

                    //数据库查询语句
                    string sql = "select stuid from student where stuname ='" + name + "'stumajor = '" + major + "';";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    ID = Convert.ToInt64(cmd.ExecuteScalar());

                    return ID;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return -1;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取课程id
        /// <summary>
        /// 在有课程名与上课学期的条件下，获取课程ID
        /// </summary>
        /// <param name="classname"></param>
        /// <param name="term"></param>
        /// <returns></returns>
        public static int GetClassInformationID(string classname, string term)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    int num;

                    //数据库查询语句
                    string sql = "select classid from class where clssaname ='" + classname + "' and term = '" + term + "';";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    num = Convert.ToInt32(cmd.ExecuteScalar());

                    return num;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return -1;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取课程表
        /// <summary>
        /// 根据学生学号获取相应的课程表
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static DataSet GetCourse(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    DataSet ds = new DataSet();

                    string sql = "select DISTINCT clssaname,sctime,location,teacher from classdetail,class,sc,student where student.stuid = " + ID + ";";

                    //ds.Tables.Add(new DataTable());

                    SqlDataAdapter adp = new SqlDataAdapter(sql, conn);

                    adp.Fill(ds);

                    return ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取学生classid
        /// <summary>
        /// 获取已知专业，已知学期的某个同学的学号获取其相应课程的课程ID
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="classname"></param>
        /// <param name="term"></param>
        /// <param name="major"></param>
        /// <returns></returns>
        public static int GetStuInformationClassID(long ID, string classname, string term, string major)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    int num;

                    //数据库查询语句
                    string sql = "select classid from class,student where clssaname = '" + classname + "' and stuname = '" + GetStuInformationName(ID) + "'and term = '" + term + "and stumajor = '" + major + "';";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    num = Convert.ToInt32(cmd.ExecuteScalar());

                    return num;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return -1;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }

        //重载
        /// <summary>
        /// 获取已知专业，已知学期的某个同学的学号获取其相应课程的课程ID
        /// </summary>
        /// <param name="name"></param>
        /// <param name="classname"></param>
        /// <param name="term"></param>
        /// <param name="major"></param>
        /// <returns></returns>
        public static int GetStuInformationClassID(string name, string classname, string term, string major)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    int num;

                    //数据库查询语句
                    string sql = "select stuid,classid from class,student where clssaname = '" + classname + "' and stuname = '" + name + "'and term = '" + term + "and stumajor = '" + major + "';";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    num = Convert.ToInt32(cmd.ExecuteScalar());

                    return num;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return -1;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 修改学生分数
        /// <summary>
        /// 修改某专业的一名学生的一个课程的分数
        /// </summary>
        /// <param name="classname"></param>
        /// <param name="ID"></param>
        /// <param name="term"></param>
        /// <param name="major"></param>
        /// <param name="score"></param>
        public static void ManChangeStuSources(string classname, long ID, string term, string major, int score)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    //sql 语句
                    string sqlchagestupassword = "update sc set scores = " + score + " where sc.classid = " + GetStuInformationClassID(ID, classname, term, major) + " and sc.stuid = " + ID + "";
                    SqlDataAdapter adp = new SqlDataAdapter(sqlchagestupassword, conn);

                    cmd.CommandText = sqlchagestupassword;
                    cmd.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }

        //重载
        /// <summary>
        /// 只知道学生姓名下的重载，修改某专业的一名学生的一个课程的分数
        /// </summary>
        /// <param name="classname"></param>
        /// <param name="name"></param>
        /// <param name="term"></param>
        /// <param name="major"></param>
        /// <param name="score"></param>
        public static void ManChangeStuSources(string classname, string name, string term, string major, int score)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    string sqlchagestupassword = "update sc set scores = " + score + " where sc.classid = " + GetStuInformationClassID(name, classname, term, major) + " and sc.stuid = " + GetStuInformationID(name, major) + "";
                    SqlDataAdapter adp = new SqlDataAdapter(sqlchagestupassword, conn);
                    cmd.CommandText = sqlchagestupassword;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取学生所有
        /// <summary>
        /// 获取学生所有信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static DataSet GetStuInformation(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    DataSet ds = new DataSet();
                    string sql = "select stuid as 学号, stuname 姓名, stugrade as 年级, stumajor as 专业, stusex as 性别,role as 身份 from student where stuid = " + ID + "";

                    SqlDataAdapter adp = new SqlDataAdapter(sql, conn);

                    adp.Fill(ds);
                    return ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取管理员所有
        /// <summary>
        /// 获取管理员所有
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static DataSet GetManInformation(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    DataSet ds = new DataSet();
                    string sql = "select manid as 账号, manname as 姓名, role as 身份 from manager where manid = " + ID + ";";

                    SqlDataAdapter adp = new SqlDataAdapter(sql, conn);

                    adp.Fill(ds);

                    return ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取管理员ID和姓名
        /// <summary>
        /// 获取管理员ID和姓名
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static DataSet GetManInformation_(long ID)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    DataSet ds = new DataSet();
                    string sql = "select manid, manname from manager where manid = " + ID + ";";

                    SqlDataAdapter adp = new SqlDataAdapter(sql, conn);

                    adp.Fill(ds);

                    return ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 修改学生信息

        #region 修改学生姓名
        /// <summary>
        /// 修改学生姓名
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="name"></param>
        public static void ManChangeStuInformation_Name(long ID, string name)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //数据库查询语句
                    string sql = "update student SET stuname = '" + name + "' where stuid = " + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }

                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 修改学生专业
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="major"></param>
        public static void ManChangeStuInformation_Major(long ID, string major)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //数据库查询语句
                    string sql = "update student SET stumajor = '" + major + "' where stuid = " + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 修改学生年级
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="grade"></param>
        public static void ManChangeStuInformation_grade(long ID, string grade)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //数据库查询语句
                    string sql = "update student SET stugrade = '" + grade + "' where stuid = " + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 修改学生专业
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="sex"></param>
        public static void ManChangeStuInformation_Sex(long ID, string sex)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //数据库查询语句
                    string sql = "update student SET stusex = '" + sex + "' where stuid = " + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #endregion

        #region 修改管理员信息
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="name"></param>
        public static void ManChangeManInformation_Name(long ID, string name)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };

                try
                {
                    //数据库查询语句
                    string sql = "update manager SET manname = '" + name + "' where manid = " + ID + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }

                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取上课地点
        /// <summary>
        /// 获取上课地点
        /// </summary>
        /// <param name="time"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public static DataSet GetStuSourceLocation(string time, string location)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };

                try
                {
                    string sql = "select * from classdetail where sctime = '" + time + "'and location = '" + location + "'";
                    SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);

                    return ds;
                }

                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 添加上课信息 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="term"></param>
        /// <param name="teacher"></param>
        /// <param name="location"></param>
        /// <param name="time"></param>
        /// <param name="credit"></param>
        public static void AddClassDetailInformation(string source, string term, string teacher, string location, string time, int credit)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;

                    //数据库查询语句
                    //string sql = "select classid from class where clssaname = '" + source + "' and term = '" + term + "' and teacher = '" + teacher + "'";
                    //cmd.CommandText = sql;
                    //long id1 = Convert.ToInt32(cmd.ExecuteScalar());
                    //MessageBox.Show(id1.ToString());
                    //string id1 = cmd.ExecuteScalar().ToString();
                    //int id = 0;
                    //int.TryParse(id1, out id);
                    string sql1 = "select max(classdetailid) from classdetail";
                    cmd.CommandText = sql1;
                    int idclassdetail = Convert.ToInt32(cmd.ExecuteScalar());
                    idclassdetail++;
                    sql1 = "select max(classid) from class";
                    cmd.CommandText = sql1;
                    //取得课程号
                    int idclass = Convert.ToInt32(cmd.ExecuteScalar());
                    idclass++;
                    //加入
                    string sql = "insert into class values(" + idclass + ",'" + source + "','" + term + "','" + teacher + "'," + credit + ")";
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    sql = "insert into classdetail values(" + idclassdetail + "," + idclass + ",'" + time + "','" + location + "')";
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 录入成绩
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="grade"></param>
        /// <param name="major"></param>
        /// <param name="source"></param>
        /// <param name="score"></param>
        public static void AddScores(string name, string grade, string major, string source, int score)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //数据库查询语句
                    string sql = "update sc set scores = " + score + " where sc.classid = " + GetStuInformationClassID(GetStuInformationID(name, major), source, grade, major) + " and sc.stuid = " + GetStuInformationID(name, major) + ";";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    //更新数据库语句
                    cmd.CommandText = sql;
                    //更新数据库
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取某专业所有学生姓名
        /// <summary>
        /// 获取某专业所有学生的姓名
        /// </summary>
        /// <param name="major"></param>
        /// <returns></returns>
        public static DataSet GetStuSourceLocation_Name(string major)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    string sql = "select stuname from student where stumajor = '" + major + "';";
                    SqlDataAdapter adp = new SqlDataAdapter(sql, conn);

                    DataSet ds = new DataSet();
                    adp.Fill(ds);

                    return ds;
                }

                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }

        }
        #endregion

        #region 获取某专业所有同学成绩单
        /// <summary>
        /// 获取某专业所有同学一学期的成绩单
        /// </summary>
        /// <param name="major"></param>
        /// <param name="term"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static DataSet GetAllStuScores(string major, string term, string source)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    DataSet ds = new DataSet();
                    string sql;
                    //SqlCommand cmd = new SqlCommand(sql, conn);
                    //String id1 = cmd.ExecuteScalar().ToString();
                    //int stuid = 0;
                    //int.TryParse(id1, out stuid);
                    //用到两个数据库的连接操作

                    sql = "select student.stuname as 姓名, sc.scores as 分数 from sc, student,class where sc.stuid = student.stuid and student.stumajor = '" + major + "' and class.term = '" + term + "' and class.clssaname = '" + source + "' and sc.classid = class.classid";
                    SqlDataAdapter adp1 = new SqlDataAdapter(sql, conn);
                    adp1.Fill(ds);
                    //载入基本信息
                    return ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取最高分
        /// <summary>
        /// 获取最高分
        /// </summary>
        /// <param name="major"></param>
        /// <param name="term"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int ManGetMaxScore(string major, string term, string source)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    int score = 0;

                    string sql = sql = "select max(sc.scores) from sc, student,class where sc.stuid = student.stuid and student.stumajor = '" + major + "' and class.term = '" + term + "' and class.clssaname = '" + source + "' and sc.classid = class.classid";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    score = Convert.ToInt32(cmd.ExecuteScalar());

                    return score;
                }

                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return -1;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取最低分
        /// <summary>
        /// 获取最低分
        /// </summary>
        /// <param name="major"></param>
        /// <param name="term"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int ManGetMinScore(string major, string term, string source)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    int score = 0;
                    string sql = sql = "select min(sc.scores) from sc, student,class where sc.stuid = student.stuid and student.stumajor = '" + major + "' and class.term = '" + term + "' and class.clssaname = '" + source + "' and sc.classid = class.classid";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    score = Convert.ToInt32(cmd.ExecuteScalar());

                    return score;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return -1;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取平均分
        public static int ManGetAvgScore(string major, string term, string source)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    int score = 0;

                    string sql = sql = "select avg(sc.scores) from sc, student,class where sc.stuid = student.stuid and student.stumajor = '" + major + "' and class.term = '" + term + "' and class.clssaname = '" + source + "' and sc.classid = class.classid";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    score = Convert.ToInt32(cmd.ExecuteScalar());

                    return score;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return -1;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 检查课程信息
        /// <summary>
        /// 检查课程是否存在
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool ManSourceCheck(string source)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    //设置数据库语句
                    string sqlcheckidsource = "select DISTINCT clssaname from sc class where class.clssaname = '" + source + "' and sc.classid = class.classid";
                    //在SQL数据库查询相关信息
                    SqlDataAdapter adp = new SqlDataAdapter(sqlcheckidsource, conn);
                    //建立DataSet类ds，用于储存信息
                    DataSet ds = new DataSet();
                    ds.Clear();
                    //填充相关信息
                    adp.Fill(ds);

                    //如果查询到相关信息，即ds.Tables[0].Rows.Count不为0 返回TRUE，反之返回FALSE
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return false;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion

        #region 获取课程详细信息
        /// <summary>
        /// 获取课程详细信息
        /// </summary>
        /// <param name="term"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static DataSet ManGetAllSourceDetail(string term, string source)
        {
            using (SqlConnection conn = new SqlConnection(stuservers.strcon))
            {
                if (conn.State == ConnectionState.Closed)
                {
                    //打开数据库
                    conn.Open();
                };
                try
                {
                    DataSet ds = new DataSet();
                    string sql;
                    //SqlCommand cmd = new SqlCommand(sql, conn);
                    //String id1 = cmd.ExecuteScalar().ToString();
                    //int stuid = 0;
                    //int.TryParse(id1, out stuid);
                    //用到两个数据库的连接操作
                    sql = "select class.clssaname as 课程名, class.teacher as 授课教师, classdetail.sctime as 课程时间, classdetail.location as 上课教室 from classdetail, class where class.term = '" + term + "' and class.clssaname = '" + source + "' and classdetail.classid = class.classid;";
                    SqlDataAdapter adp1 = new SqlDataAdapter(sql, conn);
                    adp1.Fill(ds);
                    //载入基本信息
                    return ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        //关闭数据库
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
        #endregion
    }
}